<?php
class Product_model extends Model {
	/**
	 * 添加景点
	 * @param unknown_type $cols
	 */
	public function create($cols) {
		$dProduct = new Product_data();
		
		return $dProduct->create($cols);
	}
	/**
	 * 获取景点基本信息
	 * @param unknown_type $params
	 */
	public function productInfo($params) {
		$dProduct = new Product_data();
		
		return $dProduct->productInfo($params);
	}
	/**
	 * 编辑景点
	 * @param unknown_type $id
	 * @param unknown_type $cols
	 */
	public function edit($id, $cols) {
		$dProduct = new Product_data();
		
		$params = array('id' => $id);
		return $dProduct->edit($params, $cols);
	}
	/**
	 * 删除景点
	 * @param unknown_type $node
	 */
	public function del($params) {
		$dProduct = new Product_data();
		
		return $dProduct->del($params);
	}
	/**
	 * 根据UID获取用户信息
	 * @param unknown_type $uid
	 */
	public function productInfoByid($id) {
		$dProduct = new Product_data();
	
		$params = array('id in' => $id);
		$result = $dProduct->productInfos($params);
		if(!$result) return false;
		
		$return = array();
		foreach($result as $product) {
			$return[$product['id']] = $product;
		}
		return $return;
	}
	/**
	 * 分页获取景点
	 * @param unknown_type $page
	 * @param unknown_type $size
	 * @param unknown_type $params
	 */
	public function page($page = 1, $size = 20, $params = array()) {
		$dProduct = new Product_data();
		$num = $dProduct->count($params);
		if($num === false) return false;
		if($num == 0) return array('num' => $num, 'data' => array(), 'page' => $page, 'size' => $size);
		
		$data = $dProduct->page($page, $size, $params, $orderby = '`id` DESC');
		if($data === false) return false;
		
		//获取景点分组信息
		$cid = array();
		foreach ($data as $key => $value) {
			$cid[] = $value['cid'];
		}
		$_params['cid in'] = $cid;
		$result = $this->category($_params);
		if(!$result) return false;
		foreach ($data as $key => $value) {
			$data[$key]['category'] = $result[$value['cid']];
			unset($data[$key]['cid']);
		}
		return array('num' => $num, 'data' => $data, 'page' => $page, 'size' => $size);
	}
	/**
	 * 分页获取分类
	 * @param unknown_type $page
	 * @param unknown_type $size
	 * @param unknown_type $params
	 */
	public function categoryPage($page = 1, $size = 20, $params = array()) {
		$dProduct = new Product_data();
		$num = $dProduct->categoryCount($params);
		if($num === false) return false;
		if($num == 0) return array('num' => $num, 'data' => array(), 'page' => $page, 'size' => $size);
		
		$data = $dProduct->categoryPage($page, $size, $params, $orderby = '`cid` DESC');
		if($data === false) return false;
		
		return array('num' => $num, 'data' => $data, 'page' => $page, 'size' => $size);
	}
	/**
	 * 添加分类
	 * @param unknown_type $cols
	 */
	public function categoryCreate($cols) {
		$dProduct = new Product_data();
		
		return $dProduct->categoryCreate($cols);
	}
	/**
	 * 编辑分类
	 * @param unknown_type $cid
	 * @param unknown_type $params
	 */
	public function editCategory($cid, $cols) {
		$dProduct = new Product_data();
		
		$params = array('cid' => $cid);
		return $dProduct->editCategory($params, $cols);
	}
	/**
	 * 获取分类信息
	 * @param unknown_type $params
	 */
	public function categoryInfo($params) {
		$dProduct = new Product_data();
		
		return $dProduct->categoryInfo($params);
	}
	/**
	 * 删除分类
	 * @param unknown_type $params
	 */
	public function delCategory($params) {
		$dProduct = new Product_data();
		
		return $dProduct->delCategory($params);
	}
	/**
	 * 批量获取分类
	 * @param unknown_type $params
	 */
	public function category($params = array()) {
		$dProduct = new Product_data();
		
		$result = $dProduct->category($params);
		$return = array();
		foreach($result as $key => $value) {
			$return[$value['cid']] = $value;
		}
		return $return;
	}
}